package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ZmessageBoard;
import cn.wolfcode.domain.ZmessageReply;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.service.IZmessageBoardService;
import cn.wolfcode.service.IZmessageReplyService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/zmessageBoard")
public class ZmessageBoardController {

    @Autowired
    private IZmessageBoardService zmessageBoardService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    @Autowired
    private IZmessageReplyService zmessageReplyService;

    @RequiredPermission(name = "留言板页面", expression = "zmessageBoard:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<ZmessageBoard> pageInfo = zmessageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //把字典目录全部查询出来
        model.addAttribute("systemDictionarys", systemDictionaryService.listAll());
        model.addAttribute("business",systemDictionaryItemService.queryBySn("business"));
        return "zmessageBoard/list";
    }


    @RequiredPermission(name = "留言板删除", expression = "zmessageBoard:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            zmessageBoardService.delete(id);
        }
        return "redirect:/zmessageBoard/list";
    }


    @RequiredPermission(name = "留言板新增/编辑", expression = "zmessageBoard:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ZmessageBoard zmessageBoard) {
        if (zmessageBoard.getId() == null) {
            zmessageBoardService.save(zmessageBoard);
        } else {
            zmessageBoardService.update(zmessageBoard);
        }
        return "redirect:/zmessageBoard/list";
    }


    @RequiredPermission(name = "留言板新增/编辑", expression = "zmessageBoard:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("zmessageBoard", zmessageBoardService.get(id));
        }
        return "zmessageBoard/input";
    }

    @RequestMapping("/look")
    public String look(Long id, Model model) {
        //根据id查留言
        ZmessageBoard zmessageBoard = zmessageBoardService.selectById(id);
        model.addAttribute("zmessageBoard",zmessageBoard);

        //根据id查留言的所有回复
        List<ZmessageReply> data = zmessageReplyService.selectById(id);
        model.addAttribute("data",data);
        return "zmessageBoard/input";
    }
}
